<?php
/**
 * Each engineer has a duty to keep the code elegant
 * Created by wolffy.
 * Author @wolffy<13750088090@sina.cn>
 * Date: 2018/8/6
 * Time: 19:56
 */
namespace App\Http\Controllers\Api\V1_9_3;

use App\Http\Requests\Api\V1_9_3\MobileExistsRequest;
use App\Http\Requests\Api\V1_9_3\UserForgetPasswordRequest;
use App\Http\Requests\Api\V1_9_3\UserRegisterRequest;
use App\Http\Requests\Api\V1_9_3\VerificationCodeRequest;
use App\Services\AppVersionUpgradesService;
use App\Services\Area\AreaService;
use App\Services\CloudspSMS;
use App\Services\PassportService;
use App\Services\UserService;
use Illuminate\Http\Request;

class UserController extends ApiController
{
    protected $passportService;
    protected $userService;
    protected $appVersionUpgradesService;
    protected $areaService;

    public function __construct(PassportService $passportService,
                                UserService $userService,
                                AppVersionUpgradesService $appVersionUpgradesService,
                                AreaService $areaService)
    {
        $this->passportService = $passportService;
        $this->userService = $userService;
        $this->appVersionUpgradesService = $appVersionUpgradesService;
        $this->areaService = $areaService;
    }

    /**
     * 获取三级省市区数据
     * @return \Illuminate\Http\JsonResponse
     */
    public function getPcaData()
    {
        $area = $this->areaService->getPCACode();
        return $this->responseJson(0, json_decode($area, true));
    }

    /**
     * 文案
     * @return \Illuminate\Http\JsonResponse
     */
    public function wordPress()
    {
        $data = [
            'service_charge' => [
                'withdraw_service_charge' => 2,
                'repayment_withdraw_service_charge' => 1,
            ],
            'agreement' => [
                'privacy' => config('app.url') . '/user/privacy_agreement',//隐私协议
                'server' => config('app.url') . '/user/server_agreement',//服务协议
            ],
            'help_kf' => config('app.url') . '/question/index.html',
            'collect_money_is_hidden' => 0,//收银是否隐藏
            'credit_card_handle_is_hidden' => 0,//办卡是否隐藏
            'collect_money_is_hidden_android' => 0,//收银是否隐藏
            'credit_card_handle_is_hidden_android' => 0,//办卡是否隐藏
            'user_safe_url' => 'https://m.libao.cn/#/sticsale/details?pro=89',//账户安全险
            'all_repayment_service_recharge' => [
                'level_1' => 0.006029,
                'level_2' => 0.006029,
                'level_3' => 0.006738,
                'level_4' => 0.007751,
            ],
            'repayment_question' => config('app.url') . '/question/questionType/temp/1.html',//还款金问题
            'totoro_college' => 'https://www.baidu.com',//龙猫学院
        ];
        return $this->responseJson(0, $data);
    }

    public function memberProfitDescription()
    {
        $data = [
            'level_2' => [
                'profit' => [
                    'upgrade_to_level_2_profit_percent' => [
                        'direct' => '198',
                        'indirect' => '0',
                    ],
                    'repayment_profit_percent' => [
                        'direct' => '0.6',
                        'indirect' => '0.25',
                    ],
                    'cash_profit_percent' => [
                        'direct' => '0.6',
                        'indirect' => '0.25',
                    ],
                    'credit_card_profit_percent' => [
                        'direct' => '0.6',
                        'indirect' => '0.25',
                    ],
                ],
                'personal_rate' => [
                    'repayment' => [
                        'rate' => '0.0048',
                        'every_time_price' => '1',
                    ],
                    'cash' => [
                        'rate' => '0.0055',
                        'every_time_price' => '2',
                    ],
                ]
            ],
            'level_3' => [
                'profit' => [
                    'upgrade_to_level_2' => [
                        'direct' => '198',
                        'indirect' => '0',
                    ],
                    'repayment_profit_percent' => [
                        'direct' => '0',
                        'indirect' => '0',
                    ],
                    'cash_profit_percent' => [
                        'direct' => '0',
                        'indirect' => '0',
                    ],
                    'credit_card_profit_percent' => [
                        'direct' => '0',
                        'indirect' => '0',
                    ],
                ],
                'personal_rate' => [
                    'repayment' => [
                        'rate' => '0.0055',
                        'every_time_price' => '1',
                    ],
                    'cash' => [
                        'rate' => '0.0055',
                        'every_time_price' => '2',
                    ],
                ]
            ],
            'level_4' => [
                'profit' => [
                    'upgrade_to_level_2' => [
                        'direct' => '198',
                        'indirect' => '0',
                    ],
                    'repayment_profit_percent' => [
                        'direct' => '0',
                        'indirect' => '0',
                    ],
                    'cash_profit_percent' => [
                        'direct' => '0',
                        'indirect' => '0',
                    ],
                    'credit_card_profit_percent' => [
                        'direct' => '0',
                        'indirect' => '0',
                    ],
                ],
                'personal_rate' => [
                    'repayment' => [
                        'rate' => '0.0065',
                        'every_time_price' => '1',
                    ],
                    'cash' => [
                        'rate' => '0.0058',
                        'every_time_price' => '2',
                    ],
                ]
            ],
        ];
        return $this->responseJson(0, $data);
    }

    /**
     * app版本升级
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function appVersionUpgrades(Request $request)
    {
        if (empty($request->app_type)) {
            return $this->responseJson(422);
        }
        return $this->appVersionUpgradesService->getUpgradesInfo($request->app_type);
    }

    /**
     * 时间接口
     * @return \Illuminate\Http\JsonResponse
     */
    public function getTime()
    {
        return $this->responseJson(0, [
            'date' => date('Y-m-d'),
            'year1' => date('Y'),
            'year2' => date('y'),
            'month' => date('m'),
            'day' => date('d'),
            'month-day' => date('m-d'),
            'monthday' => date('md'),
            'date_time' => date('Y-m-d H:i:s'),
            'timestamp' => time(),
        ]);
    }

    /**
     * 注册接口
     * @param UserRegisterRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function register(UserRegisterRequest $request)
    {
        return $this->userService->registerUser($request->mobile, $request->password, $request->verification_code, $request->invited_code);
    }

    /**
     * 登录接口
     * @param Request $request
     * @param PassportService $passportService
     * @return \Illuminate\Http\JsonResponse
     */
    public function login(Request $request)
    {
        return $this->passportService->authUserToLogin($request);
    }

    /**
     * 发送短信验证码
     * 注册和忘记密码
     * @param CloudspSMS $cloudspSMS
     * @param VerificationCodeRequest $request
     * @param $mobile
     * @param $content
     * @return array|\Illuminate\Http\JsonResponse
     */
    public function verificationCode(CloudspSMS $cloudspSMS, VerificationCodeRequest $request)
    {
        $mobile = $request->mobile;
        $content = __randStr();
        if ('register' == $request->type) {
            return $cloudspSMS->sendSMS($mobile, $content, 'register', 1);
        } else if ('forget_password' == $request->type) {
            return $cloudspSMS->sendSMS($mobile, $content, 'forgetPassword', 2);
        } else if ('forget_pay_password' == $request->type) {
            return $cloudspSMS->sendSMS($mobile, $content, 'forgetPayPassword', 2);
        } else if ('truename_auth' == $request->type) {
            return $cloudspSMS->sendSMS($mobile, $content, 'truenameAuth', 3);
        }  else if ('bind_bank_card' == $request->type) {
            return $cloudspSMS->sendSMS($mobile, $content, 'bindBankCard', 4);
        }  else if ('bind_credit_card' == $request->type) {
            return $cloudspSMS->sendSMS($mobile, $content, 'bindCreditCard', 5);
        }  else if ('credit_card_handle' == $request->type) {
            return $cloudspSMS->sendSMS($mobile, $content, 'creditCardHandle', 6);
        } else {
            return $this->responseJson(401);
        }
    }

    /**
     * 查找手机号码是否被占用
     * @param MobileExistsRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function findMobileIsExists(MobileExistsRequest $request)
    {
        return $this->userService->findMobileIsExists($request->mobile);
    }

    /**
     * 忘记密码
     */
    public function forgetPassword(UserForgetPasswordRequest $request)
    {
        return $this->userService->updatePassword($request->mobile, $request->password, $request->verification_code);
    }

}